package com.yaozheng.vocationaltraining.activity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ItemLongClick;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.apache.commons.lang.ObjectUtils;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ListView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.adapter.CourseCatalogAdapter;
import com.yaozheng.vocationaltraining.adapter.MyCollectAdapter;
import com.yaozheng.vocationaltraining.dialog.ask.ConfirmCancelAskDialog;
import com.yaozheng.vocationaltraining.iview.ILoadMoreView;
import com.yaozheng.vocationaltraining.iview.collect.IMyCollectDeleteView;
import com.yaozheng.vocationaltraining.iview.collect.IMyCollectListView;
import com.yaozheng.vocationaltraining.manage.ListViewLoadMoreManage;
import com.yaozheng.vocationaltraining.service.collect.MyCollectDeleteService;
import com.yaozheng.vocationaltraining.service.collect.MyCollectListService;
import com.yaozheng.vocationaltraining.service.impl.collect.MyCollectDeleteServiceImpl;
import com.yaozheng.vocationaltraining.service.impl.collect.MyCollectListServiceImpl;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 我的收藏
 *
 * @author Administrator
 */
@EActivity(R.layout.activity_news_list)
public class MyCollectActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, ILoadMoreView, IMyCollectListView, IMyCollectDeleteView {
    @ViewById
    ListView listView;
    int lastId;

    ListViewLoadMoreManage listViewLoadMoreManage;

    MyCollectAdapter myCollectAdapter;


    /**
     * 下拉刷新
     */
    @ViewById
    SwipeRefreshLayout swipeRefreshLayout;

    @Bean(MyCollectListServiceImpl.class)
    MyCollectListService myCollectListService;
    @Bean(MyCollectDeleteServiceImpl.class)
    MyCollectDeleteService myCollectDeleteService;

    int currPosition;


    @AfterViews
    public void initView() {
        setHeadTitle("我的收藏");
        myCollectListService.init(this);
        myCollectDeleteService.init(this);
//        swipeRefreshLayout.postDelayed(new Runnable() {
//
//            @Override
//            public void run() {
//                swipeRefreshLayout.setRefreshing(true);
//                onRefresh();
//
//            }
//        }, 500);
        onRefresh();
        swipeRefreshLayout.setOnRefreshListener(this);
        listViewLoadMoreManage = new ListViewLoadMoreManage(this,
                this, listView);
    }

    public void loadData() {
        myCollectListService.getMyCollectList(lastId);
    }

    @Override
    public void cancelLoadDataProgressDialog() {
        swipeRefreshLayout.setRefreshing(false);
        listViewLoadMoreManage.loadMoreComplete();
    }

    @UiThread
    @Override
    public void myCollectListSuccess(JSONObject jsonObject) {
        cancelLoadDataProgressDialog();
        updateData(TypeUtils.getJsonArray(jsonObject, "data", "list"));
    }

    @UiThread
    @Override
    public void myCollectListError(String error) {
        cancelLoadDataProgressDialog();
        alertMessage(error);
    }

    @Override
    public void loadMoreData() {
        if (myCollectAdapter != null) {
            int lastId = myCollectAdapter.getLastId();
            if (lastId > 0) {
                this.lastId = lastId;
                loadData();
            }
        }
    }

    @Override
    public void onRefresh() {
        lastId = 0;
        loadData();
    }

    /**
     * 修改数据
     */
    public void updateData(JSONArray dataList) {
        if (dataList == null) return;
        if (myCollectAdapter == null) {
            myCollectAdapter = new MyCollectAdapter(this, dataList);
            listView.setAdapter(myCollectAdapter);
        } else {
            if (lastId == 0) {
                myCollectAdapter.getDataList().clear();
            }
            myCollectAdapter.getDataList().addAll(dataList);
            myCollectAdapter.notifyDataSetChanged();
        }
    }

    @ItemClick(R.id.listView)
    public void listViewItemClick(int position) {
        if (myCollectAdapter != null) {
            if (myCollectAdapter != null) {
                JSONObject courseCatalogJsonObject = TypeUtils.getJsonObject(
                        myCollectAdapter.getDataList(), position);
                Intent intent = new Intent();
                intent.putExtra("title", TypeUtils.getJsonString(courseCatalogJsonObject, "targetSubject"));
                intent.putExtra("courserJsonStr",
                        courseCatalogJsonObject.toString());
                intent.putExtra("isLoadNew", true);
                intent.putExtra("subCatId", 5);
//                if ("video".equals(TypeUtils.getJsonString(courseCatalogJsonObject,
//                        "format"))) {
//
//                    intent.setClassName(this,
//                            Constants.VIDEO_COURSEWARE_DETAIL_ACTIVITY_CLASS_NAME);
//
//                } else if ("image".equals(TypeUtils.getJsonString(
//                        courseCatalogJsonObject, "format"))) {
                intent.setClassName(
                        this,
                        Constants.IMAGE_LIST_COURSEWARE_DETAIL_ACTIVITY_CLASS_NAME);
//                } else
//                    return;
                startActivity(intent);
            }
        }
    }

    @ItemLongClick(R.id.listView)
    public void listViewItemLongClick(int position) {
        this.currPosition = position;
        showConfirmCancelAskDialog("是否取消收藏？");
    }


    /**
     * 确定取消 询问对话确定点击
     */
    public void confirmCancelAskDialogDetermineClick() {
        deleteCollectOperating(currPosition);
    }

    /**
     * 删除收藏
     *
     * @param position
     */
    public void deleteCollectOperating(int position) {
        if (myCollectAdapter != null) {
            showOperatingProgressDialog("取消收藏中...");

            JSONObject itemJsonObject = myCollectAdapter.getItem(position);
            myCollectDeleteService.deleteCollect(TypeUtils.getJsonInteger(itemJsonObject, "id"));
        }
    }

    @UiThread
    @Override
    public void deleteCollectSuccess(JSONObject jsonObject) {
        cancelOperatingProgressDialog();
        if (myCollectAdapter != null) {
            myCollectAdapter.getDataList().remove(currPosition);
            myCollectAdapter.notifyDataSetChanged();
            onRefresh();
        }
    }

    @UiThread
    @Override
    public void deleteCollectError(String error) {
        cancelOperatingProgressDialog();
        alertMessage(error);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
